package cn.liumouren.jzxd.controller;

import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.R;
import cn.liumouren.jzxd.entity.Tag;
import cn.liumouren.jzxd.service.TagService;
import cn.liumouren.jzxd.utils.QueryUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * @author Freeman Liu
 * @email llw599502537@gmail.com
 * @date 2020-10-31 00:44:45
 */
@RestController
@RequestMapping("/tag")
@Api(tags = "标签")
public class TagController {
    @Autowired
    private TagService tagService;

    @ApiOperation("万能查询")
    @RequestMapping(value = "/customQuery", method = RequestMethod.GET)
    public R getSceneNames(@RequestParam Map<String, Object> map) {
        PageUtils result = QueryUtil.getResult(tagService, map);
        return R.ok("查询成功", result);
    }

    /**
     * 列表
     */
    @ApiOperation("分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tagService.queryPage(params);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @ApiOperation("根据id查询")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public R info(@PathVariable("id") String id) {
        Tag tag = tagService.getById(id);

        return R.ok().put("data", tag);
    }

    /**
     * 保存
     */
    @ApiOperation("新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public R save(@RequestBody Tag tag) {
        tagService.save(tag);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public R update(@RequestBody Tag tag) {
        tagService.updateById(tag);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("批量删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public R delete(@RequestBody String[] ids) {
        tagService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @ApiOperation("根据column及其对应值查找指定tag")
    @RequestMapping(value = "/getTagByColumn", method = RequestMethod.POST)
    public R getTagByColumn(@RequestBody Map<String, Object> map) {
        Tag tag = tagService.getTagByColumn(map);
        StringBuilder str = new StringBuilder();
        String s;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            s = entry.getKey() + "=" + entry.getValue();
            if (StringUtils.isEmpty(str.toString())) {
                str.append(s);
            } else {
                str.append(",").append(s);
            }
        }
        return R.ok("成功获取[" + str.toString() + "]的标签", tag);
    }

}
